package com.message.notify;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setTitle("消息提醒通知");
    }

    /**
     * toast消息提醒
     * @param view
     */
    public void toastClick(View view){
        Toast.makeText(this, "Toast消息提醒", Toast.LENGTH_SHORT).show();
    }

    /**
     * notification消息通知
     * @param view
     */
    @SuppressLint("WrongConstant")
    public void notificationClick(View view){
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = null;
        String channelId = "com.message.notify";
        String channelName = "notification";
        //判断api版本大于等于26
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel =new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(this, channelId);
        }else {
            builder = new NotificationCompat.Builder(this);
            builder.setChannelId(channelId);
        }
        builder.setContentTitle("消息通知标题")//设置消息通知标题
                .setContentText("消息通知内容")//设置消息通知内容
                .setWhen(System.currentTimeMillis())//设置消息通知发送时间
                .setSmallIcon(R.mipmap.app_icon)//设置小童通知状态栏图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon))//设置消息通知下拉图标
                .setPriority(NotificationCompat.PRIORITY_MAX);//设置消息优先级

        //设置消息通知点击意图
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);

        //点击消息通知后删除的两种实现方法
        //第一种
        builder.setAutoCancel(true);
        //第二种
        int notificationId = 1;
//        manager.cancel(notificationId);

        //设置消息通知的提醒效果
        builder.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luan.ogg")))//设置消息声音
                .setVibrate(new long[]{0, 1000, 1000, 1000})//设置消息震动，需要加入android.permission.VIBRATE权限
                .setLights(Color.GREEN, 1000, 1000)//设置消息提醒灯
                .setDefaults(NotificationCompat.DEFAULT_ALL);//设置默认效果

        //设置悬挂弹出
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        builder.setFullScreenIntent(pendingIntent, true);

        Notification notification = builder.build();
        manager.notify(notificationId, notification);
    }
}